﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.TradeChannel.Gyb.Request;
using GN.Pay.TradeChannel.ProtocolPay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb
{
    /// <summary>
    /// 贵阳银行协议支付
    /// </summary>
    [Serializable]
    public class GybProtocolPayService : GybServiceBase, IProtocolPay
    {
        /// <summary>
        /// 
        /// </summary>
        public override string ChannelId
        {
            get
            {
                return "Gyb.ProtocolPay";
            }
        }

        public override string ChannelName
        {
            get
            {
                return "贵阳银行协议支付";
            }
        }

        public override ChannelType ChannelType
        {
            get
            {
                return ChannelType.ALL;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        public bool BankCardBindValidate(IProtocolBindApply apply)
        {
            if (string.IsNullOrWhiteSpace(apply.MemberId))
            {
                this.ThrowChannelValidationException("会员编号不能为空");
            }
            if (string.IsNullOrWhiteSpace(apply.IdentityNo))
            {
                this.ThrowChannelValidationException("身份证号不能为空");
            }
            if (string.IsNullOrWhiteSpace(apply.AccountNo))
            {
                this.ThrowChannelValidationException("银行卡号或账号不能为空");
            }
            if (string.IsNullOrWhiteSpace(apply.AccountName))
            {
                this.ThrowChannelValidationException("户名不能为空");
            }
            if (string.IsNullOrWhiteSpace(apply.MobilePhone))
            {
                this.ThrowChannelValidationException("预留手机号不能为空");
            }
            //  return true;
            ProtocolValidateRequest req = new ProtocolValidateRequest(this, apply, this.channelConfigure);
            var res = req.Build(this);           
            if (!res.SignVeryfy())
            {                
                TradeChannelExceptionUtils.ThrowClientRequestException(this.ChannelId, "验证未通过："+ res.ErrorMsg);
            }
            return res.IsSuccess();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        public IProtocolBindApplyResult BankCardBind(IProtocolBindApply apply)
        {
            if (!BankCardBindValidate(apply))
            {
                this.ThrowChannelValidationException("绑定验证未通过");
            }

            //return new DefaultImpl.ProtocolBindApplyResult()
            //{
            //    AccountName = apply.AccountName,
            //    AccountNo = apply.AccountNo,
            //    AccountType = BankAccountType.Debit,
            //    BankNo = "GYB",
            //    CreditCardLastNo = null,
            //    CreditCardLastTerm = null,
            //    IdentityNo = apply.IdentityNo,
            //    MemberId = apply.MemberId,
            //    MobilePhone = apply.MobilePhone,
            //    OpenBankCity = null,
            //    OpenBankName = null,
            //    ProtocolNo = Guid.NewGuid().ToString("N"),
            //    State = ProtocolApplyResultState.Success,
            //    StateMessage = "申请成功"
            //};

            var req = new Request.ProtocolBindRequest(this, apply, this.channelConfigure);
            var res = req.Build(this);
            if (!res.IsSuccess())
            {
                if (res.ErrorMsg == null)
                {
                    res.ErrorMsg = "未知";
                }
                if (string.IsNullOrWhiteSpace(res.ErrorMsg))
                {
                    throw this.ThrowInterfaceException("申请失败:" + res.ErrorMsg);
                }
                throw this.ThrowInterfaceException("申请失败,响应代码:" + res.ErrorCode + "，消息:" + res.ErrorMsg);
            }
            if (!res.SignVeryfy())
            {
                TradeChannelExceptionUtils.ThrowClientRequestException(this.ChannelId, "验证未通过");
            }
            return res.ToResult(apply);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        public IProtocolApplyResult UnBankCardBind(IProtocolUnBindApply apply)
        {
            var req = new ProtocolUnBindRequest(this, apply, this.channelConfigure);
            var res = req.Build(this, this.channelConfigure);
            if (!res.IsSuccess())
            {
                if (res.ErrorMsg == null)
                {
                    res.ErrorMsg = "未知";
                }
                return new ProtocolApplyResult() { State = ProtocolApplyResultState.Fail, ProtocolNo = apply.ProtocolNo, StateMessage = res.ErrorMsg };
            }
            if (!res.SignVeryfy())
            {
                TradeChannelExceptionUtils.ThrowClientRequestException(this.ChannelId, "验签未通过");
            }
            return new ProtocolApplyResult() { State = ProtocolApplyResultState.Success, ProtocolNo = apply.ProtocolNo, StateMessage = "成功" };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public override ITradePayApplyResult PayApply(IOrderPayApply order)
        {
            IProtocolPayApply shortcut = order as IProtocolPayApply;
            if (shortcut == null)
            {
                this.ThrowChannelValidationException("付款申请未实现 IShortcutPayApply 接口");
            }
            if (string.IsNullOrWhiteSpace(shortcut.ProtocolNo))
            {
                this.ThrowChannelValidationException("协议号不能为空或未签约");
            }
            var config = this.channelConfigure;

            //var result= new TradeOrderQueryResult(this)
            //{
            //    OrderNo = order.OrderNo,
            //    PayTime = DateTime.Now,
            //    TradeNo = Guid.NewGuid().ToString("N"),
            //    OrderMoney = order.OrderMoney,
            //    Subject = "未知",
            //    Body = "未知",
            //    ExtraParam = "",
            //    OtherPartyAccount = "",
            //    OtherPartyAccountId = "",
            //    PayFee = 0m,
            //    RefundFee = 0m,
            //    RefundMoney = 0m
            //};

            //return new TradePayApplyResult("", "", "", TradePayApplyResultCallType.PaySuccess, result);

            var request = new ProtocolPayRequest(this, order, shortcut, config);
            return request.Build();
        }

    }
}
